<template>
  <div class="app-container">
    <game-type-list @onSelected="getSelectList" :opens="handleSelect"></game-type-list>

    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="兑换码" prop="cdkey">
        <el-input
            v-model="queryParams.cdkey"
            placeholder="请输入兑换码"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="游戏类别" prop="categroyId">
        <el-input
            v-model="queryParams.categroyId"
            placeholder="请输入游戏类别"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="兑换情况" prop="isRedeemed">
        <el-select style="width: 200px" v-model="queryParams.isRedeemed" placeholder="请选择是否已被兑换" clearable>
          <el-option
              v-for="dict in zhen_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['activity:gift:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['activity:gift:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['activity:gift:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['activity:gift:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="giftList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="礼包码ID" align="center" prop="cdkeyId" />
      <el-table-column label="兑换码" align="center" prop="cdkey" />
      <el-table-column label="游戏类别ID" align="center" prop="categoryName" />
      <el-table-column label="是否已被兑换" align="center" prop="isRedeemed">
        <template #default="scope">
          <dict-tag :options="zhen_disable" :value="scope.row.isRedeemed"/>
        </template>
      </el-table-column>
      <el-table-column label="礼包内容" align="center" prop="content" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['activity:gift:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['activity:gift:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改礼包码对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="giftRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item disabled="d" label="兑换码" prop="cdkey">
          <el-input  v-model="form.cdkey" placeholder="请输入兑换码" />
        </el-form-item>


        <el-form-item label="游戏类型" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="请选择游戏分类">
            <template #append>
              <el-button @click="openSelect">
                选择
              </el-button>
            </template>
          </el-input>
        </el-form-item>


        <el-form-item label="是否已被兑换" prop="isRedeemed">
          <el-select v-model="form.isRedeemed" placeholder="请选择是否已被兑换">
            <el-option
                v-for="dict in zhen_disable"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="礼包内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Gift">
import { listGift, getGift, delGift, addGift, updateGift } from "@/api/activity/gift";
import gameTypeList from "@/components/gameList/gameListType.vue";


const handleSelect = ref(false)
// const cancelSelect = function () {
//   handleSelect.value = false
// }
const openSelect = function () {
  handleSelect.value = true
}
const getSelectList = function (e) {
  let a = toRaw(e);
  console.log(a,"data")
  form.value.categroyId = a.categoryId
  form.value.platform = a.platform
  form.value.categoryName = a.categoryName
  handleSelect.value = false
}





const { proxy } = getCurrentInstance();
const { zhen_disable } = proxy.useDict('zhen_disable');

const giftList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    cdkey: null,
    categroyId: null,
    isRedeemed: null,
  },
  rules: {
    cdkey: [
      { required: true, message: "兑换码不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询礼包码列表 */
function getList() {
  loading.value = true;
  listGift(queryParams.value).then(response => {
    giftList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    cdkeyId: null,
    cdkey: null,
    categroyId: null,
    isRedeemed: null,
    content: null,
    createTime: null
  };
  proxy.resetForm("giftRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.cdkeyId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加礼包码";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _cdkeyId = row.cdkeyId || ids.value
  getGift(_cdkeyId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改礼包码";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["giftRef"].validate(valid => {
    if (valid) {
      if (form.value.cdkeyId != null) {
        updateGift(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addGift(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _cdkeyIds = row.cdkeyId || ids.value;
  proxy.$modal.confirm('是否确认删除礼包码编号为"' + _cdkeyIds + '"的数据项？').then(function() {
    return delGift(_cdkeyIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('activity/gift/export', {
    ...queryParams.value
  }, `gift_${new Date().getTime()}.xlsx`)
}

getList();
</script>
